Welcome in the PCTG R Markdown template. This document describes how the template looks like, building an interactive Manhattan plot.
I would suggest having an exec summary at the beginning to help readers understand what you have done
R markdown creates interactive reports from R code. This post provides a few tips I use on a daily basis to improve the appearance of output documents. In any case, an unavoidable resource is the Rstudio documentation.
can also have hyperlinks to the applicable section in the important points through For example, this link
R markdown allows to easily format your text. You can add links, write in bold or italic. This is very well explained in the Rstudio cheatsheet.
Here is the code used to make this paragraph:
R markdown allows to easily format your text. You can add [links](www.r-graph-gallery.com), write in **bold** or *italic*. This is very well explained in the [Rstudio cheatsheet](https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf).Add an horizontal line by adding 3 stars:
***Header of level 1, 2, 3 are set using #, ## and ###. You can auto number your chapters using this option in the header:
---
title: "Your title"
output:
html_document:
number_sections: TRUE
---
# Title
## A subtitle
## Another subtitle
# Another titleI really like to add spaces in my document to give it a more
uncluttered look. This is done using the <br>
command. This .rmd code:
A first sentence
<br><br><br><br>
A seconde sentencewill give this htmloutput:
A first sentence
A seconde sentence
To center an image, use this code:
<center>

</center>FigName
If you’re struggling with to much white space around your image, try
to play with the fig.asp argument. Here I used
fig.asp=0.50.
library(png)
library(grid)
img <- readPNG("kan.png")
grid.raster(img)Lovely Kangaroo from the Mornington Peninsula.
Note: thanks to Anna Quaglieri for this suggestion.
Specify the caption of your figure in the chunk header. Example:
{r, fig.align="center", fig.width=6, fig.height=6, fig.cap="Figure: Here is a really important caption."}library(tidyverse)
mpg %>%
ggplot( aes(x=reorder(class, hwy), y=hwy, fill=class)) +
geom_boxplot() +
xlab("class") +
theme(legend.position="none")Figure: Here is a really important caption.
Change the black default caption using CSS. Adding this code in your
style.css file.
<style>
p.caption {
font-size: 0.9em;
font-style: italic;
color: grey;
margin-right: 10%;
margin-left: 10%;
text-align: justify;
}
</style>Figure: Here is a really important caption, customized to be grey and in italic.
Insert equation using Latex formating:
\(A = (\pi * \lambda \times r^{4}) / \alpha\)
Delimit Latex syntax with $
$A = (\pi * \lambda \times r^{4}) / \alpha $You can display 2 plots one beside each other. Add
out.width=c('50%', '50%'), fig.show='hold' to your chunk
header. Something like:
``{r out.width=c('50%', '50%'), fig.show='hold'}
boxplot(1:10)
plot(rnorm(10))
`
Since R Markdown use the bootstrap
framework under the hood. It is possible to benefit its powerful
grid system. Basically, you can consider that your row is divided in 12
subunits of same width. You can then choose to use only a few of this
subunits.
Here, I use 3 subunits of size 4 (4x3=12). The last column is
used for a plot. You can read more about the grid system here. I got this result showing the
following code in my R Markdown document.
Code to get this result:
<div class = "row">
<div class = "col-md-4">
<br><br>Since R Markdown use the [bootstrap framework](https://getbootstrap.com/docs/4.0/layout/grid/) under the hood. It is possible to benefit its powerful grid system. Basically, you can consider that your row is divided in 12 subunits of same width. You can then choose to use only a few of this subunits.
</div>
<div class = "col-md-4">
<br><br>Here, I use 3 subunits of size 4 (4x3=12). The last column is used for a plot. You can read more about the grid system [here](bootstrap grid system). I got this result showing the following code in my R Markdown document.
</div>
<div class = "col-md-4">
``{r, message=FALSE, echo=FALSE}
ggplot( mtcars, aes(x=mpg)) + geom_histogram(fill="skyblue", alpha=0.5) + theme_minimal()
``
</div>
</div>The DT library is my favourite option to display tables in your document. It allows to:
Here is an example with the options I use most of the time:
library(DT)
datatable(mtcars, rownames = FALSE, filter="top", options = list(pageLength = 5, scrollX=T) )You can apply some css to a specific part of your
document. Here is an example where I change the background color of a
small part. Handy to highlight conclusions at the end of your
document.
Code:
<style>
div.blue { background-color:#e6f0ff; border-radius: 5px; padding: 20px;}
</style>
<div class = "blue">
- This is my first conclusion
- This is my second conclusion
</div>Since R Markdown can output an html document, it is possible to apply whatever techniques used in common website. It is thus possible to set a header with a background image in parallax. See an example online here. Here is a glimpse of how it looks like:
You need to custom the css and the
header.html files of your document. See the code provided
in the corresponding
github repository.
It is possible to cache a specific chunk adding
cache=TRUE in its header. You can also cash the whole
document adding knitr::opts_chunk$set(cache=TRUE) in a
chunk at the begining of the document.
Use this option with care, I strongly advise to read this document by Yihui
Xie on this topic.
However, this is great for running heavy computations / large datasets / large plots that you will not need to rerun regularly
You can use internal links in R Markdown using anchors. For example, this link will bring you to the previous chapter. To do it:
# You can link to the cached section {#cache}For example, [this link](#cache) will bring ...The anchor described in the previous section can be called directly in the URL. This URL will bring you directly to this section:
https://holtzy.github.io/Pimp-my-rmd/#Anchor-URL-linkR allows to build any type of interactive
graphic. My favourite library is plotly
that will turn any of your ggplot2 graphic interactive in one
supplementary line of code. Try to hover points, to select a zone, to
click on the legend.
library(ggplot2)
library(plotly)
library(gapminder)
p <- gapminder %>%
filter(year==1977) %>%
ggplot( aes(gdpPercap, lifeExp, size = pop, color=continent)) +
geom_point() +
scale_x_log10() +
theme_bw()
ggplotly(p)You can use any of the bootswatch theme to automatically custom the font and general appearance of your document. You can also control the syntax highlighting style. See the possibilty here. Here is how to call these options in the YAML header:
title: "your title"
output:
html_document:
theme: sandstone
highlight: tangoDisplay your html online using github. Follow these steps:
.rmd file index.rmd. Knit it to
have a index.html file..html file will be available at username.github.io/repoNameAn attempt to illustrate it with a few screenshots:
Explanation in the R Markdown Websites section of the R Markdown documentation. Note that this website is render using this technic. To run a basic example:
library(rmarkdown)rmarkdown::render_site()_site folder that contains an
index.html file. Open it, it is your website. You can host
it on github like explained above.Note that you can go further using the blogdown package to generate static websites based on R Markdown and Hugo. This allows to build awesome websites only with R. It is used by a huge number of R blogs now.
It is a good practice to add a session info at the end of your document. It will increase reproducibility and costs only one line of code
sessionInfo()## R version 4.2.1 (2022-06-23)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur ... 10.16
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] gapminder_0.3.0 plotly_4.10.0 DT_0.23 forcats_0.5.1
## [5] stringr_1.4.0 dplyr_1.0.9 purrr_0.3.4 readr_2.1.2
## [9] tidyr_1.2.0 tibble_3.1.8 ggplot2_3.3.6 tidyverse_1.3.2
## [13] bibtex_0.4.2.3 epuRate_0.1 rmarkdown_2.14
##
## loaded via a namespace (and not attached):
## [1] lubridate_1.8.0 assertthat_0.2.1 digest_0.6.29
## [4] utf8_1.2.2 R6_2.5.1 cellranger_1.1.0
## [7] backports_1.4.1 reprex_2.0.1.9000 evaluate_0.15
## [10] highr_0.9 httr_1.4.3 pillar_1.8.0
## [13] rlang_1.0.4 lazyeval_0.2.2 googlesheets4_1.0.0
## [16] readxl_1.4.0 data.table_1.14.2 rstudioapi_0.13
## [19] jquerylib_0.1.4 labeling_0.4.2 googledrive_2.0.0
## [22] htmlwidgets_1.5.4 munsell_0.5.0 broom_1.0.0
## [25] compiler_4.2.1 modelr_0.1.8 xfun_0.31
## [28] pkgconfig_2.0.3 htmltools_0.5.3 tidyselect_1.1.2
## [31] viridisLite_0.4.0 fansi_1.0.3 crayon_1.5.1
## [34] tzdb_0.3.0 dbplyr_2.2.1 withr_2.5.0
## [37] grid_4.2.1 jsonlite_1.8.0 gtable_0.3.0
## [40] lifecycle_1.0.1 DBI_1.1.3 magrittr_2.0.3
## [43] scales_1.2.0 cli_3.3.0 stringi_1.7.8
## [46] cachem_1.0.6 farver_2.1.1 fs_1.5.2
## [49] xml2_1.3.3 bslib_0.4.0 ellipsis_0.3.2
## [52] generics_0.1.3 vctrs_0.4.1 tools_4.2.1
## [55] glue_1.6.2 crosstalk_1.2.0 hms_1.1.1
## [58] fastmap_1.1.0 yaml_2.3.5 colorspace_2.0-3
## [61] gargle_1.2.0 rvest_1.0.2 knitr_1.39
## [64] haven_2.5.0 sass_0.4.2
Special thanks to:
A work by Dr
Conal Monaghan
Special Thanks to Yan Holtz (https://github.com/holtzy/) for the RMD template and design
Research supported by the School of Medicine and Psychology (SMP), Australian National University (ANU)